package com.xonro.common.histrans.sfeg.biz.cm;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.emm.util.DateUtil;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.base.consts.SfegConst;
import com.xonro.common.histrans.sfeg.biz.CommTransBiz;
import com.xonro.common.histrans.sfeg.biz.common.SfegProcessTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class ContractMoneyBiz  extends SfegProcessTransBiz {
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();
        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_CFM_CONTRACT_PAY");//合同资金支付
        logModel.setOuterTable("tb_ContractMoneyInOutAll");//合同资金支付
        logModel.setOuterPK(mainData.getString("id"));
        logModel.setOuterExt1(mainData.getString("cmoCode"));
        logModel.setOuterExt2(mainData.getString("ctCode"));//合同编号
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("APPLY_NO", mainData.getString("cmoCode"));//申请单号
        mainBO.set("CONTRACT_NO", mainData.getString("ctCode"));//合同编号

        mainBO.set("CONTRACT_SETTLE_PRICE", mainData.getString("realTotal"));//合同结算价

        mainBO.set("DEPARTMENT_NAME", mainData.getString("dealDept"));//申请部门
        mainBO.set("APPLY_DATE", DateUtil.parse(mainData.getString("filldate"),"yyyy-MM-dd"));//申请时间
        mainBO.set("USER_NAME", CommTransBiz.getInstance().getUserId( mainData.getString("dealUser")));//申请人
        mainBO.set("BUSINESS_TYPE", mainData.getString("feeType"));//业务类型

        mainBO.set("INVOICE_AMOUNT", mainData.getString("applyInvoiceTotal"));//开票金额
        mainBO.set("APPLY_AMOUNT", mainData.getString("applyOutTotal"));//本次申请金额(元)
        mainBO.set("REMARK", mainData.getString("bz"));//备注
        mainBO.set("APPROVAL_PAY_AMOUNT", mainData.getString("acceptMoney"));//本次批准金额(元)
        mainBO.set("TOTAL_DEDUCTION_AMOUNT", mainData.getString("kkje"));//扣款金额  累计扣款小计
        mainBO.set("INVOICE_AMOUNT", mainData.getString("realInvoiceTotal"));//开票金额
        mainBO.set("REAL_PAY_AMOUNT", mainData.getString("realOutTotal"));//实际支付金额




        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);

        //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //印章id
        fileTransData.setFkCode(mainData.getString("enCode"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_CFM_CONTRACT_PAY");
        fileTransData.setBoName("BO_XR_CFM_CONTRACT_PAY");
        fileTransData.setFieldName("ATTACHMENT");//印张图片
    }


    @Override
    protected String getProcessDefId() {
        return "obj_d12245e9be544c9295fa12bb422dd4c1";
    }

    @Override
    protected String getActivityDefId() {
        return "obj_f29cf6a329d24c5bb1d2d661ce8cf2f3";
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        String enCode = ctx.getMainData().getString("cmoCode");
        String itemName = ctx.getMainData().getString("itemName");
        return itemName+enCode;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        return ctx.getMainData().getString("cmoCode");
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_ContractMoneyInOutAll";
    }

    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        return historyTransContext.getMainData().getString("cmoCode");
    }

    @Override
    protected List<RowMap> getMainDatas() {
        return SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMaps("SELECT * FROM tb_ContractMoneyOut out LEFT JOIN tb_ContractMoneyInOutAll a ON a.enCode= out.cmoCode WHERE out.applyDept LIKE '1040%' AND out.cmoCode in (SELECT cmoCode FROM tb_ContractMoneyOut out  LEFT JOIN tb_ContractMoneyInOutAll a ON a.enCode= out.cmoCode WHERE out.applyDept LIKE '1040%' and a.id is  null)");
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }
}
